<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class LevelController extends AdminBaseController {

    /**
     * 等级分布报表
     */
    public function index(){
        $this->setTitle('活跃等级分布');

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart  = new PubChart();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-31 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addExport();

        $params = $filter->get();

        $header = LevelService::indexHeader();
        $data   = LevelService::indexData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('活跃等级分布', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $chartConfig = LevelService::indexChart($data);
            $chart->addChart('chart', $chartConfig['data'], array(
                'name'   => "最近三日的等级分布图",
                'type'   => 'bar',
            ));

            $grid->addGrid($header, $data, array(
                'name'  => "多日等级分布",
                'order' => array('RecDate', 'desc'),
            ));

            $this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();
            $this->out['chart'] = $chart->show();
        }
    }

    public function details(){
        $this->setTitle('活跃等级分布(明细)');

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart  = new PubChart();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addExport();

        $params = $filter->get();

        $header = LevelService::detailsHeader();
        $data   = LevelService::detailsData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('活跃等级分布(明细)', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'order' => array('Level', 'asc'),
                'showAll' => 1,
            ));

            $chartData = LevelService::detailsChart($data);
            $chart->addChart('chart', $chartData, array(
                'name'   => "活跃等级分布",
                'type'   => 'bar',
            ));

            $this->out['filter']    = $filter->show();
            $this->out['grid']      = $grid->show();
            $this->out['chart']     = $chart->show();
        }
    }

    public function role(){
        $this->setTitle('各职业等级分布');

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart = new PubChart();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time', 'input', '日期', array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('min_level', 'input', '等级区间', array('value' => 1))
            ->addFilter('max_level', 'input', '至', array('value' => 80))
            ->addExport();

        $params = $filter->get();

        $header = LevelService::roleHeader($params);
        $data   = LevelService::roleData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('等级分布', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $chartData = LevelService::roleChart($data, $params);
            $chart->addChart('chart', $chartData, array(
                'name'   => "各职业等级分布",
                'type'   => 'bar',
            ));

            $grid->addGrid($header, $data, array(
                'order'     => array('Role', 'desc'),
                'showAll'   => 1
            ));

            $this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();
            $this->out['chart'] = $chart->show();
        }
    }

    public function distribution(){
        $this->setTitle(PubFunc::t('等级分布报表'));

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart = new PubChart();


        $stateOptions = array('ALL' => PubFunc::t('等级分布'));

        $kpiOptions = LevelService::getDistributionDict();

        $filter->addPlatformFilter()
            ->addServerFilter()
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',time() - 86400), 'date' => 'day'))
            ->addFilter('kpi', 'link', PubFunc::t('KPI指标'), array('options' => $kpiOptions, 'value' => 'AllUser', 'fixed'=>true))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'));
        $params = $filter->get();

        $header = LevelService::distributionHeader($params);
        $data   = LevelService::distributeData($params);
        $maxPercent = PubStat::getMaxPercent($data,"RoleUserRate");
        $chartConfig = LevelService::distributeChartData($data,$params);

        $label = $kpiOptions[$params['kpi']];
        $chart->addChart('chart', $chartConfig['data'], array(
            'name'   => $label . PubFunc::t("等级分布"),
            'type'   => 'line',
            'config'=> array(
                'series' => array(
                    '__default' => array(
                        'itemStyle' => array(
                            'normal' => array(
                                'areaStyle' => array('type' => 'default')
                            )
                        )
                    )
                )
            )

        ));
        foreach($stateOptions as $k=>$v){
            if($k == $params['state']){
                $grid->addGrid($header, $data, array(
                    'name'      => $v,
                    'length'    => 200,
                    'order' => array(
                        array('RecDate', 'desc'),
                        array('RoleLevel', 'asc')
                    ),
                ), array("maxPercent"=>$maxPercent));
            }else{
                $grid->addLink($k, $v);
            }
        }

        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
        $this->out['chart'] = $chart->show();
    }

    public function independent(){
        $this->setTitle(PubFunc::t('各等级分布报表'));

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart = new PubChart();

        $kpiOptions = LevelService::getDistributionDict();
        $kpiOptions['LoginUser'] = PubFunc::t('登录玩家');
        $stateOptions = array('ALL' => PubFunc::t('各等级分布'));

        $filter->addPlatformFilter()
            ->addServerFilter(true, true)
            ->addFilter('time', 'timerange', PubFunc::t('日期'), array('value' => PubDateRange::getLastDayRange(3, strtotime('yesterday'))))
            ->addFilter('kpi', 'link', PubFunc::t('KPI指标'), array('options' => $kpiOptions, 'value' => 'AllUser', 'fixed'=>true))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'));
        $params = $filter->get();

        $header = LevelService::independentHeader($params);
        $data   = LevelService::independentData($params);
        $chartConfig = LevelService::independentChart($data,$params);
        $chart->addChart('chart', $chartConfig['data'], array(
            'name'   => PubFunc::t("各等级分布"),
            'type'   => 'line',
            'config'=> array(
                'series' => array(
                    '__default' => array(
                        'itemStyle' => array(
                            'normal' => array(
                                'areaStyle' => array('type' => 'default')
                            )
                        )
                    )
                )
            )
        ));
        foreach($stateOptions as $k=>$v){
            if($k == $params['state']){
                $grid->addGrid($header, $data, array(
                    'name'  => $v,
                    'order' => array('RecDate', 'desc'),
                ));
            }else{
                $grid->addLink($k, $v);
            }
        }

        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
        $this->out['chart'] = $chart->show();
    }
    public function independentKPI(){
        $this->setTitle(PubFunc::t('指标等级分布对比报表'));

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart = new PubChart();

        $kpiOptions = LevelService::getDistributionDict();
        $kpiOptions['LoginUser'] = PubFunc::t('登录玩家');
        $stateOptions = array('ALL' => PubFunc::t('各等级分布'));

        $filter->addPlatformFilter()
            ->addServerFilter(true, true)
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',time() - 86400), 'date' => 'day'))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'));
        $params = $filter->get();

        $header = LevelService::independentKPIHeader($params);
        $data   = LevelService::independentKPIData($params);
        $chartConfig = LevelService::independentKPIChart($data,$params);
        $chart->addChart('chart', $chartConfig['data'], array(
            'name'   => PubFunc::t("各等级分布"),
            'type'   => 'line',
            'config'=> array(
                'series' => array(
                    '__default' => array(
                        'itemStyle' => array(
                            'normal' => array(
                                'areaStyle' => array('type' => 'default')
                            )
                        )
                    )
                )
            )
        ));
        foreach($stateOptions as $k=>$v){
            if($k == $params['state']){
                $grid->addGrid($header, $data, array(
                    'name'  => $v,
                    'order' => array('RecDate', 'desc'),
                ));
            }else{
                $grid->addLink($k, $v);
            }
        }

        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
        $this->out['chart'] = $chart->show();
        $this->tpl = "level_independent.tpl";
    }

    public function active(){
        $this->setTitle('活跃段位分布');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addExport();

        $params = $filter->get();

        $header = LevelService::activeHeader($params);
        $data   = LevelService::activeData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('活跃段位分布', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'order' => array('ActivityId', 'asc')
            ));

            $this->out['filter'] = $filter->show();
            $this->out['chart']  = $chart->show();
            $this->out['grid']   = $grid->show();
        }
    }
} 
